<?php
namespace app\common\service;

use think\facade\Request;
use think\Response;

class BaseService
{
    /**
     * 返回数据的标准格式
     * @param  any      $data 待返回的数据
     * @param  integer  $code 错误码
     * @param  string   $msg  提示信息
     * @param  integer  $http_code http状态码
     * @return
     */
    public static function parseToData($data = null, $code = 0, $msg = 'ok', $http_code = 200)
    {
        $responseData = [
            'data' => $data,
            'code' => intval($code),
            'msg'  => $msg,
        ];
        // 响应格式解析
        $format   = Request::param('format');
        $format   = ($format && in_array(strtolower($format), ['xml', 'json', 'jsonp'])) ? $format : 'json';
        $response = Response::create($responseData, $format)->code($http_code);

        return $response;
    }
}
